package com.zbu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbu.reggie.common.CustomException;
import com.zbu.reggie.dto.SetmealDto;
import com.zbu.reggie.entity.Setmeal;
import com.zbu.reggie.entity.SetmealDish;
import com.zbu.reggie.mapper.SetmealMapper;
import com.zbu.reggie.service.SetmealDishService;
import com.zbu.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /*新增套餐
     * 套餐表和套餐菜品关系表*/
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息 setmeal
        this.save(setmealDto);//框架保存之后给他一个id
        //保存套餐和菜品的关联信息 setmeal_dish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();//集合里面没有套餐id
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).toList();

        setmealDishService.saveBatch(setmealDishes);

    }

    /*删除套餐，同时删除套餐与菜品的关联数据
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {

        //查询套餐状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);//售卖中 不可以删除

        int count = this.count(wrapper);
        if (count > 0) {
            //不能删除抛出异常
            throw new CustomException("套餐正在售卖中，不可以删除");
        }
        //可以删除 删除套餐表中的数据
        this.removeByIds(ids);
        //删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> dishwrapper = new LambdaQueryWrapper<>();
        dishwrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(dishwrapper);


    }

    /**
     * 根据套餐id查询套餐中的菜品
     */
    public SetmealDto getByIdWithDish(long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    /**
     * 修改菜品
     */
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐表的套餐
        this.updateById(setmealDto);

        //查询并删除旧的套餐中的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //更新传过来的新的菜品，并将其赋予套餐的id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //将新传过来的套餐中的菜品其保存到setmeal_dish表中
        setmealDishService.saveBatch(setmealDishes);
    }



}
